//
//  RankAndFiltrateView.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/15.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "RankAndFiltrateView.h"
#import "UIView+ComboBoxExtension.h"
#define kTitleColor WB_RGBColor(126, 119, 102)
#define kTitleSelectColor WB_RGBColor(48, 111, 227)
#define kTitleSize [UIFont systemFontOfSize:15]

@interface RankAndFiltrateView()
{
    NSArray *buttons;
}
@property (strong, nonatomic) UIButton *lastButton;


@end

@implementation RankAndFiltrateView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        buttons =[NSArray array];
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    //等级
    UIButton *button = [UIButton ba_buttonWithFrame:CGRectMake(0, 0, self.mm_width / 5,  self.mm_height) title:@"等级" titleColor:kTitleColor titleFont:kTitleSize image:[UIImage imageNamed:@"pulldown"] backgroundColor:[UIColor clearColor]];
    [button setImage:WB_ImageName(@"shaixuan_down") forState:UIControlStateNormal];
    [button setImage:WB_ImageName(@"shaixuan_up") forState:UIControlStateSelected];
    [button ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeCenterImageRight padding:2.0f];
    button.tag = 1000;
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//    [button setTitleColor:kTitleSelectColor forState:UIControlStateSelected];
    [self addSubview:button];
    
    //利率
    UIButton *button1 = [UIButton ba_buttonWithFrame:CGRectMake(self.mm_width / 5, 0, self.mm_width / 5,  self.mm_height) title:@"利率" titleColor:kTitleColor titleFont:kTitleSize image:nil backgroundColor:[UIColor clearColor]];
    [button1 setImage:WB_ImageName(@"shaixuan_down") forState:UIControlStateNormal];
    [button1 setImage:WB_ImageName(@"shaixuan_up") forState:UIControlStateSelected];
    [button1 ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeCenterImageRight padding:2.0f];
    button1.tag = 1001;
    [button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//    [button1 setTitleColor:kTitleSelectColor forState:UIControlStateSelected];
    [self addSubview:button1];
    
    //期限
    UIButton *button2 = [UIButton ba_buttonWithFrame:CGRectMake(self.mm_width / 5 * 2, 0, self.mm_width / 5,  self.mm_height) title:@"期限" titleColor:kTitleColor titleFont:kTitleSize image:nil backgroundColor:[UIColor clearColor]];
    [button2 setImage:WB_ImageName(@"shaixuan_down") forState:UIControlStateNormal];
    [button2 setImage:WB_ImageName(@"shaixuan_up") forState:UIControlStateSelected];
    [button2 ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeCenterImageRight padding:2.0f];
    button2.tag = 1002;
    [button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//    [button2 setTitleColor:kTitleSelectColor forState:UIControlStateSelected];
    [self addSubview:button2];
    
    //进度
    UIButton *button3 = [UIButton ba_buttonWithFrame:CGRectMake(self.mm_width / 5 * 3, 0, self.mm_width / 5,  self.mm_height) title:@"进度" titleColor:kTitleColor titleFont:kTitleSize image:nil backgroundColor:[UIColor clearColor]];
    [button3 setImage:WB_ImageName(@"shaixuan_down") forState:UIControlStateNormal];
    [button3 setImage:WB_ImageName(@"shaixuan_up") forState:UIControlStateSelected];
    [button3 ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeCenterImageRight padding:2.0f];
    button3.tag = 1003;
    [button3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//    [button3 setTitleColor:kTitleSelectColor forState:UIControlStateSelected];
    [self addSubview:button3];
    
    //竖线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 25)];
    line.mm_centerX = button3.mm_right;
    line.mm_centerY = self.mm_height / 2;
    line.backgroundColor = WB_RGBColor(240, 240, 240);
    [self addSubview:line];
    
    //筛选
    UIButton *button4 = [UIButton ba_buttonWithFrame:CGRectMake(self.mm_width / 5 * 4, 0, self.mm_width / 5,  self.mm_height) title:@"筛选" titleColor:WB_RGBColor(48, 111, 227) titleFont:kTitleSize image:[UIImage imageNamed:@"筛选"] backgroundColor:[UIColor clearColor]];
    [button4 ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeCenterImageRight padding:2.0f];
    button4.tag = 1004;
    [button4 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [button4 setTitleColor:kTitleSelectColor forState:UIControlStateSelected];
    [self addSubview:button4];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.mm_height - 0.5, self.mm_width, 0.5)];
    line2.backgroundColor = WB_RGBColor(240, 240, 240);
    [self addSubview:line2];
    
    buttons = @[button,button1,button2,button3];
}

- (void)click:(UIButton *)sender
{
    if (sender != _lastButton) {
        [sender setTitleColor:kTitleSelectColor forState:UIControlStateNormal];
        [_lastButton setTitleColor:kTitleColor forState:UIControlStateNormal];
        _lastButton.selected = NO;

    }
    sender.selected = !sender.selected;
    _lastButton = sender;
    if (sender.tag == 1000) {
        self.sortField = @"level";
    }else if (sender.tag == 1001) {
        self.sortField = @"rate";
    }else if (sender.tag == 1002) {
        self.sortField = @"periods";
    }else if (sender.tag == 1003) {
        self.sortField = @"progress";
    }
    if (sender.selected) {
        self.sortType = @"1";
    }else{
        self.sortType = @"2";
    }
    
    NSLog(@"哈哈");
    if (_delegate && [_delegate respondsToSelector:@selector(clickRankView:button:)]) {
        [_delegate clickRankView:self button:sender];
        
        for (UIButton *btn in buttons) {
            btn.userInteractionEnabled = NO;
        }
        
        WB_DISPATCH_MAIN_Delay_THREAD(1, ^{
            for (UIButton *btn in buttons) {
                btn.userInteractionEnabled = YES;
            }
        })
    }
}

@end
